package org.pupil.socket.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import org.pupil.common.Cache;
import org.pupil.common.Host;
import org.pupil.common.URL;
import org.pupil.common.constants.Register;
import org.pupil.config.Properties;
import org.pupil.register.RegistryFactory;
import org.pupil.register.RegistryService;
import org.pupil.socket.codec.RpcDecoder;
import org.pupil.socket.codec.RpcEncoder;
import java.util.List;

/**
 * @Description: 服务发送方
 * @Author: pupil
 * @Date: 2024/07/09 下午 4:00
 */
public class Client {

    private  Bootstrap bootstrap;
    private  EventLoopGroup eventLoopGroup;

    // 启动服务发送方
    public void run() {
        bootstrap = new Bootstrap();
        eventLoopGroup = new NioEventLoopGroup(4);
        bootstrap.group(eventLoopGroup).channel(NioSocketChannel.class)
                .option(ChannelOption.SO_KEEPALIVE, true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        socketChannel.pipeline()
                                .addLast(new RpcEncoder())
                                .addLast(new RpcDecoder())
                                .addLast(new ClientHandler());
                    }
                });

        Cache.BOOT_STRAP = bootstrap;
    }
}
